-- banvote.lua
-- Starts a vote to have someone banned from the hub
-- Version 1.1
-- By Tyrone

---------------------------------------

--[[
Changelog:
1.1
-There is now a cooldown period of one hour
-You can now vote on propositions as well as kickbans
-You cannot ban someone for 0 or negative minutes.

1.0
-Basic functionality

]]



-- set up all command prefixes
cmdNames = {
    "vote"
}

NEWLINE = string.format("\n")
COOLDOWN = 3600 -- plugin cooldown, 0 for none
bot_name = "Democracy" --VH:GetConfig("config", "hub_security")
_,security = VH:GetConfig("config", "hub_security")
bot_desc = "bot description here"
bot_email = "bot@nowhere.com"
use_bot = false

function doCommand(nick, data, cmd)
	if (voted[UserIP(nick)] == nil) then voted[UserIP(nick)] = false end

	if (string.find(string.lower(data), "^ban")) then
		--vote to ban someone
		if (votetime > 0) then
<<<<<<< .mine
			SendMessageToUser("A vote is already running.  Wait for that one to finish first.", nick, bot_name)
=======
			SendMessageToUser("A vote is already running.  Wait for that one to finish first.", nick, bot_name)
		elseif (cooltime > 0) then
			SendMessageToUser("Votes can only be held once an hour.  Please wait "..cooltime.." seconds.", nick, bot_name)
>>>>>>> .r411
		else
			--check the syntax
<<<<<<< .mine
			_,_,instr = string.find(data, "^%s(.+)")
=======
			SendMessageToUser(data, "Aethereal", bot_name)
			_,_,instr = string.find(data, "^ban%s(.+)")
>>>>>>> .r411
			_,_,victim = string.find(instr, "^(.+)%s")
			_,_,btime = string.find(instr, "%s(%d+)")
			-- if (btime == nil) then
				-- SendMessageToUser("Wtf... data="..data.." instr="..instr.." victim="..victim.." btime=nil", nick, bot_name)
			-- end
			
			if (string.len(victim) > 0) and (string.len(btime) > 0) then
				--correct syntax
				btime = tonumber(btime)
				if (btime < 1) then
					SendMessageToUser("Don't be a retard.", nick, bot_name)
				elseif (btime > 60) and (UserClass(nick) < 3) then
					SendMessageToUser("You can't ban someone for that long.", nick, bot_name)
				else
					if (UserIsOnline(victim)) then
						--start the vote
						if (btime == 1) then minute = " minute" else minute = " minutes" end
						SendMessageToHub("A vote to kickban user: "..victim.." for "..btime..minute.." has begun!  Type .vote yes to agree to this kicking, type .vote no to disagree."..
										" Every member of the hub is free to vote!  Voting closes 5 minutes from now.", bot_name)
						votevictim = victim
						votevictimip = UserIP(victim)
						votetime = 300
						votebtime = btime
						voteprop = ""
						voted[UserIP(nick)] = true
						voteyes = 1
						if (UserClass(nick) > 3) then votescore = 0.5 else votescore = 1.2 end
					else
						--you can't vote against someone who possibly doesn't exist!
						SendMessageToUser("I can't start a vote against someone I don't even know exists!  Wait for them to come online first...", nick, bot_name)
					end
				end
			else
				doHelp(nick)
			end
		end
<<<<<<< .mine
	elseif (string.find(string.lower(data), "^proposition")) then
		if (votetime > 0) then
			SendMessageToUser("A vote is already running.  Wait for that one to finish first.", nick, bot_name)
		else
			--check the syntax
			_,_,instr = string.find(data, "%s(.+)")
			_,_,victim = string.find(instr, "^(.+)%s")
			_,_,prop = string.find(string.sub(instr, string.len(victim)), "^(.+)%s")
			--syntax checking
			if (victim == nil) or (prop == nil) then
				doHelp(nick)
				return 0
			end
			
			if (string.len(victim) > 0) and (string.len(prop) > 0) then
				if (UserIsOnline(victim)) then
					SendMessageToHub("A vote determining whether "..victim..prop.." has begun!  Type .vote yes to agree with this proposition, type .vote no to disagree."..
					                 " Every member of the hub is free to vote!  Voting closes 5 minutes from now.", bot_name)
					votevictim = victim
					votevictimip = UserIP(victim)
					votetime = 300
					votebtime = 0
					vote[UserIP(nick)] = true
					voteyes = 1
					voteprop = prop
					if (UserClass(nick) > 3) then votescore = 0.5 else votescore = 1.2 end
				else
					SendMessageToUser("I can't start a vote against someone I don't even know exists!  Wait for them to come online first...", nick, bot_name)
				end
			else
				doHelp(nick)
		else
			doHelp(nick)
=======
	elseif (string.find(string.lower(data), "^proposition")) then
		if (votetime > 0) then
			SendMessageToUser("A vote is already running.  Wait for that one to finish first.", nick, bot_name)
		elseif (cooltime > 0) then
			SendMessageToUser("Votes can only be held once an hour.  Please wait "..cooltime.." seconds.", nick, bot_name)
		else
			--check the syntax
			_,_,instr = string.find(data, "%s(.+)")
			victim = string.sub(instr, 1, string.find(instr, "%s") - 1)
			prop = string.sub(instr, string.len(victim) + 1)
			--syntax checking
			--if (victim == nil) then victim = "nil" end
			--if (prop == nil) then prop = "nil" end
			--SendMessageToUser("Wtf... data="..data.." instr="..instr.." victim="..victim.." prop="..prop, nick, bot_name)
			if (victim == nil) or (prop == nil) then
				doHelp(nick)
				return 0
			end
			
			if (string.len(victim) > 0) and (string.len(prop) > 0) then
				if (UserIsOnline(victim)) then
					SendMessageToHub("A vote determining whether "..victim..prop.." has begun!  Type .vote yes to agree with this proposition, type .vote no to disagree."..
					                 " Every member of the hub is free to vote!  Voting closes 5 minutes from now.", bot_name)
					votevictim = victim
					votevictimip = UserIP(victim)
					votetime = 300
					votebtime = 0
					voted[UserIP(nick)] = true
					voteyes = 1
					voteprop = prop
					votescore = 1
				else
					SendMessageToUser("I can't start a vote against someone I don't even know exists!  Wait for them to come online first...", nick, bot_name)
				end
			else
				doHelp(nick)
			end
		end
>>>>>>> .r411
	elseif (voteyes > 0) then
		if (string.lower(data) == "yes") then
			--voting yes
			if (votebtime == 0) then voteto = "to proposing " else voteto = "to kickbanning " end
			if (voted[UserIP(nick)] == false) then
				voted[UserIP(nick)] = true
<<<<<<< .mine
				if (UserClass(nick) > 3) then votescore = votescore + 0.5 else votescore = votescore + 1.2 end
				SendMessageToUser("You have voted YES "..voteto..votevictim.." for "..votebtime.." minutes.", nick, bot_name)
=======
>>>>>>> .r411
				voteyes = voteyes + 1
				if (votebtime > 0) then --This is a kickban vote
					if (UserClass(nick) > 3) then votescore = votescore + 0.5 else votescore = votescore + 1.2 end
					SendMessageToUser("You have voted YES to kickbanning "..votevictim.." for "..votebtime.." minutes.", nick, bot_name)
				else --this is a proposition
					votescore = votescore + 1
					SendMessageToUser("You have voted YES to whether "..votevictim..voteprop..".", nick, bot_name)
				end
				
			else
				SendMessageToUser("You have already voted in this poll.", nick, bot_name)
			end
		elseif (string.lower(data) == "no") then
			--voting no
			if (voted[UserIP(nick)] == false) then
				voted[UserIP(nick)] = true
<<<<<<< .mine
				if (UserClass(nick) > 3) then votescore = votescore - 0.4 else votescore = votescore - 0.8 end
				SendMessageToUser("You have voted NO "..voteto..votevictim.." for "..votebtime.." minutes.", nick, bot_name)
=======
>>>>>>> .r411
				voteno = voteno + 1
				if (votebtime > 0) then --This is a kickban vote
					if (UserClass(nick) > 3) then votescore = votescore - 0.4 else votescore = votescore - 0.8 end
					SendMessageToUser("You have voted NO to kickbanning "..votevictim.." for "..votebtime.." minutes.", nick, bot_name)
					
				else --This is a proposition
					votescore = votescore - 1
					SendMessageToUser("You have voted NO to whether "..votevictim..voteprop..".", nick, bot_name)
				end
			else
				SendMessageToUser("You have already voted in this poll.", nick, bot_name)
			end
		else
			doHelp(nick)
		end
	else
		SendMessageToUser("No vote is currently being held.  Type .vote help to learn how to start one of your own.", nick, bot_name)
	end
end

function doHelp(nick)
    SendMessageToUser("Usage: ", nick, bot_name)
    DisplayHelpMessage("help", "Shows this message.", nick)
    -- put coad in here for help commands:
	DisplayHelpMessage("ban <User> <time>", "Starts a vote to ban User for <time> number of Minutes", nick)
<<<<<<< .mine
	DisplayHelpMessage("proposition <User> <Proposition>", "Starts a vote to propose whether a user is something, in the form 'A vote determining whether <user> <proposition> has begun!'")
=======
	DisplayHelpMessage("proposition <User> <Proposition>", "Starts a vote to propose whether a user is something, in the form 'A vote determining whether <user> <proposition> has begun!'", nick)
>>>>>>> .r411
	if (UserClass(nick) > 3) then
		DisplayHelpMessage("Yes", "Votes yes to a currently running vote.  An operator yes vote is worth 0.5 votes.", nick)
		DisplayHelpMessage("No", "Votes yes to a currently running vote.  An operator no vote is worth 0.4 votes.", nick)
	else
		DisplayHelpMessage("Yes", "Votes yes to a currently running vote.  A yes vote is worth 1.2 votes.", nick)
		DisplayHelpMessage("No", "Votes no to a current running vote.  A no vote is worth 0.8 votes.", nick)
	end
end

-- insert other functions here
---------------------------------------
-- you can ignore below here
voted = {}
votescore = 0
votetime = 0
votevictim = ""
votevictimip = ""
votebtime = 0
voteyes = 0
voteno = 0
<<<<<<< .mine
voteprop = ""
=======
voteprop = ""
cooltime = 0
>>>>>>> .r411
tobekilled = {}

function Main()
    -- Check if script should add a new bot.
    if (use_bot) then
    -- Add new bot.
        VH:AddRobot(bot_name, 10, bot_desc, "Bot ", bot_email, "0")
    end
end

function UnLoad()
	if (use_bot) then
    	VH:DelRobot(bot_name)
	end
end

function VH_OnParsedMsgChat(nick, data)
    for i=1, table.maxn(cmdNames), 1 do
		if (string.find(data, "^%."..cmdNames[i])) then
		    _,_,cmd = string.find(data, "^%."..cmdNames[i].."%s(.+)")
		    if (cmd == "help") then
		        doHelp(nick)
		    else
        		doCommand(nick, cmd, cmdNames[i])
				--This will send the .vote into doCommand unlike the others.
				--This is a necessary change for custom votes.
        	end
    		return 0
    	end
	end
	return 1
end

function ready(nick)
    if (timeLeft[nick] == 0) then
		timeLeft[nick] = COOLDOWN
	elseif (timeLeft[nick] == nil) then
		timeLeft[nick] = COOLDOWN
	else
		SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  You have "..timeLeft[nick].." seconds to wait.", nick, bot_name)
		return true
	end
	return false
end

function KickAllUsersWithIP(ip, reason)
	_,nicklist = VH:GetNickList()
	nicklist = string.sub(nicklist, 11)
	flag = false
	for names in string.gmatch(nicklist, "([^%$]+)%$%$") do
		if (ip == UserIP(names)) then
			if (flag == false) then
				VH:KickUser(security, names, reason)
				flag = true
			else
				VH:CloseConnection(names)
			end
		end
	end
end

function SendMessageToHub(data, from)
	return VH:SendDataToAll("<"..from.."> "..data.."|",0,10)
end

function SendMessageToUser(data, nick, from)
	return VH:SendDataToUser("<"..from.."> "..data.."|", nick)
end

function SendPrivateMessageToUser(user, data, from)
    return VH:SendDataToUser("$To: "..user.." From: "..from.." $<"..from.."> "..data.."|", user);
end

function DisplayHelpMessage(cmd, msg, nick)
    SendMessageToUser("        ."..cmdNames[1].." "..cmd, nick, bot_name)
    SendMessageToUser("        "..msg, nick, bot_name)
end

function UserIsOnline(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return false
	else
		return true
	end
end

function UserIP(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return 0
	else
		return sIP
	end
end

function UserClass(nick)
	res, class = VH:GetUserClass(nick)
	if res and class then return class else return 0 end
end

function SendEmoteToHub(data, from)
	result, err = VH:SendDataToAll("<"..from.."> ".."/me "..data.."|",0,10)
	return 1
end

function VH_OnTimer()
	if (votetime > 0) then
		votetime = votetime - 1
	else
		if (voteyes > 0) then
			--votetime is zero, and people have voted
			--a vote must have just ended.
			SendMessageToHub("Voting is now CLOSED."..NEWLINE.."Poll statistics:"..NEWLINE.."YES votes: "..voteyes..NEWLINE.."NO votes: "..voteno..NEWLINE..
							"Combined Vote Rating: "..votescore, bot_name)
<<<<<<< .mine
			if (votescore > 5) then
				if (votebtime > 0) then
					if (votebtime == 1) then minute = " minute" else minute = " minutes" end
					if (UserIsOnline(votevictim)) then
						SendMessageToHub("Since the combined vote rating was greater than 5, "..votevictim.." is to be removed from the hub for "..votebtime..minute.." immediately.", bot_name)
						KickAllUsersWithIP(votevictimip, "The people have spoken _ban_"..votebtime.."m")
					else
						SendMessageToHub("Since the combined vote rating was greater than 5, "..votevictim.." is to be removed from the hub for "..votebtime..minute.." as soon as they rejoin the hub.", bot_name)
						tobekilled[votevictimip] = votebtime
					end
=======
			if (votebtime > 0) then --This is a kickban vote
				if (votescore > 5) then
					if (votebtime == 1) then minute = " minute" else minute = " minutes" end
					if (UserIsOnline(votevictim)) then
						SendMessageToHub("Since the combined vote rating was greater than 5, "..votevictim.." is to be removed from the hub for "..votebtime..minute.." immediately.", bot_name)
						KickAllUsersWithIP(votevictimip, "The people have spoken _ban_"..votebtime.."m")
					else
						SendMessageToHub("Since the combined vote rating was greater than 5, "..votevictim.." is to be removed from the hub for "..votebtime..minute.." as soon as they rejoin the hub.", bot_name)
						tobekilled[votevictimip] = votebtime
					end
>>>>>>> .r411
				else
<<<<<<< .mine
					SendMessageToHub("Since the combined vote rating was greater than 5, is has been determined that "..votevictim..voteprop.."...", bot_name)
=======
					SendMessageToHub("Since the combined vote rating was less than 5, "..votevictim.." is not to be removed from the hub and is pardoned.", bot_name)
>>>>>>> .r411
				end
<<<<<<< .mine
			else
				if (votebtime > 0) then
					SendMessageToHub("Since the combined vote rating was less than 5, "..votevictim.." is not to be removed from the hub and is pardoned.", bot_name)
				else
					SendMessageToHub("Since the combined vote rating was less than 5, the proposition whether "..votevictim..voteprop.." has been decided by the hub to be false.", bot_name)
				end
=======
			else --This is a proposition vote
				if (votescore > 0) then
					SendMessageToHub("Since the combined vote rating was greater than 0, the hub has decided that "..votevictim..voteprop.."!", bot_name)
					SendEmoteToHub(string.sub(voteprop, 2), votevictim)
				else
					SendMessageToHub("Since the combined vote rating was less than or equal to 0, the proposition whether "..votevictim..voteprop.." has been decided by the hub to be false.", bot_name)
				end
>>>>>>> .r411
			end
			votescore = 0
			votetime = 0
			voteyes = 0
			voteno = 0
			voted = {}
			votevictim = ""
			votevictimip = ""
<<<<<<< .mine
			voteprop = ""
=======
			voteprop = ""
			cooltime = COOLDOWN
>>>>>>> .r411
		else
			if (cooltime > 0) then cooltime = cooltime - 1 end
		end
	end
end

function VH_OnNewConn(ip)
	if (tobekilled[ip] == nil) then tobekilled[ip] = 0 end
	
	if (tobekilled[ip] > 0) then
		KillAllUsersWithIP(ip, "You were voted by the people of this hub to be banned while you were offline. _ban_"..tobekilled[nick].."m")
		tobekilled[ip] = 0
	end
end
